package com.ly.ql.Wms.utils;

import com.alibaba.fastjson2.JSONException;
import io.swagger.models.auth.In;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;

public class TypeSafeUtils {
   private static ReentrantLock lock = new ReentrantLock(); //使用ReenTrantLock加锁生成唯一单号
   public static String TypeToString(Object o){
       return o == null ? "" : String.valueOf(o);
   }
   public static Long typeToLong(Object o){
       if (o instanceof Number){
           return ((Integer) o).longValue();
       }
       return 0L;
   }
   public static Integer typeToInteger(Object o){

       if (o instanceof Boolean) {
        return ((Boolean) o).booleanValue() ? 1 : 0;
    }

            if (o instanceof Number) {
        return ((Number) o).intValue();
    }
            return 0;
    }

   public static BigDecimal typeToDecimal(Object o){
       if (o instanceof Byte
               || o instanceof Short
               || o instanceof Integer
               || o instanceof Long
               || o instanceof AtomicInteger
               || o instanceof AtomicLong
       ) {
           return BigDecimal.valueOf(((Number) o).longValue());
       }

       if (o instanceof Float || o instanceof Double) {
           return BigDecimal.valueOf(((Number) o).doubleValue());
       }

       if (o instanceof BigInteger) {
           return new BigDecimal((BigInteger) o);
       }
       return new BigDecimal(0);
   }
    public static String getNo(String FormId) {
        String unique = "";
        String prefix= "";
        //上锁
        lock.lock();
        try {
            //------------------流水号业务逻辑----------------
            unique = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
            //5位流水号
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放锁
            lock.unlock();
        }
        switch (FormId){
            case "PUR_ReceiveBill" : prefix="CGRk";
            default: prefix="OTHER";
        }
        return prefix + unique;
    }
}
